(function($){
	$.fn.tags = function() {


 		return this.each(function() {
 			var select = this;
 			var tagGroup = $(this).attr('data-tag-group');
 			var contentType = $(this).attr('data-content-type');
 			var token = $('#concerto-token').val();
 			var search = $('<input type="text"/>').attr('class', 'multiselect-search');
 			var results = $('<ul/>').attr('class', 'multiselect-search-results').css('display', 'none');
			var ul = $('<ul/>').attr('class', 'multiselect');
			var clearBoth = $('<div/>').css('clear', 'both');
			var createStub = $('<button/>').html('+').attr('disabled', 'disabled').addClass('multiselect-create-stub');
			$(this).after(search);
			search.after(createStub);
			createStub.click(function(e) {
				$(this).attr("disabled", 'disabled');
				e.preventDefault();
				var val = search.val();
				var rootUrl = concerto.rootUrl;
				$.ajax({
					url:rootUrl + '?view=api',
					dataType:'json',
					data:{
						'object':'tag',
						'type':'tag',
						'title':val,
						'group':tagGroup,
						'contentType':contentType,
						'concerto_token':token
					},
					type:'POST',
					success:function(data) {
						var option = $('<option/>').attr('value', data.id).attr('selected', 'selected').html(val).appendTo(select);
						var append = $('<li/>').html(val + '<a class="remove-multiselect" href="javascript:void();">X</a>').appendTo(ul);
						append.data('option', option);
						append.children('.remove-multiselect').click(function() {
							$($(this).parent('li').data('option')).attr('selected', false);
							$(this).parent('li').remove();
						});
						
					}
				});
			});
			createStub.after(clearBoth);
			
			clearBoth.after(results);
			search.css('font-style', 'italic').css('color', '#ccc').val('Search...');
			results.after(ul);
			ul.data('select', select);
			results.css('width', search.width() + 'px');
			$(this).children('option:selected').each(function() {
				var child = $('<li/>').data('option', this).html($(this).html() + '<a class="remove-multiselect" href="javascript:void();">X</a>').appendTo(ul);
				child.children('.remove-multiselect').click(function() {
					var parent = $(this).parent('li');
					$(parent.data('option')).attr('selected', false);
		
					parent.remove();
				});
			});
			$(this).hide();
			search.click(function() {
				if($(this).val() == 'Search...') {
					$(this).val('').css('font-style', 'inherit').css('color', 'inherit');
				}
			});
			search.focus(function() {
				if($(this).val() == 'Search...') {
					$(this).val('').css('font-style', 'inherit').css('color', 'inherit');
				}
			});
			search.blur(function() {
				if(!$(this).val()) {
					$(this).css('font-style', 'italic').css('color', '#ccc').val('Search...');
				}
			});	
			search.keyup(function() {
				results.html('');
				var query = $(this).val();
				if(query) {
					createStub.attr('disabled', 'disabled');
					results.show();
					$(select).find(":contains('" + query + "')").not(':selected').each(function() {
						var child = $('<li/>').html($(this).html()).appendTo(results);
						child.data('option', this);
						child.click(function() {
							var append = $('<li/>').html($(this).html() + '<a class="remove-multiselect" href="javascript:void();">X</a>').appendTo(ul);
							append.data('option', $(this).data('option'));
							append.children('.remove-multiselect').click(function() {
								$($(this).parent('li').data('option')).attr('selected', false);
								$(this).parent('li').remove();
							});
							
							$($(this).data('option')).attr('selected', 'selected');
							$(this).remove();
							search.val('');
							results.hide();
						});
					});
					if(!results.children('li').length && !$(select).find(":contains('" + query + "')").length) {
						createStub.attr('disabled', false);
					}
				}
				else {
					createStub.attr('disabled', 'disabled');
					results.hide();
					
				}
			
			});
		});
	};
})(jQuery);